package jh.com.jhappbf.dispatchcar;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import com.yzq.zxinglibrary.android.CaptureActivity;
import com.yzq.zxinglibrary.bean.ZxingConfig;
import com.yzq.zxinglibrary.common.Constant;

import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import jh.com.jhappbf.R;

import static android.app.Activity.RESULT_OK;

/**
 * author : wwh
 * e-mail : 953889276@qq.com
 * time   : 2018/9/4 15:36
 * desc   :
 */
public class ReceiptManager extends Fragment implements OnClickListener {

    private static final int REQUEST_CODE_SCAN = 0x0001;
    private RelativeLayout mRelativeLayout;
    private ImageView mQRCodeImg;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.receipt_main_activity, null);
        initView(v);
        return v;
    }

    private void initView(View v) {
        mRelativeLayout = v.findViewById(R.id.receipt_qrcode_layout);
        mQRCodeImg = v.findViewById(R.id.receipt_qrcode_img);
        mRelativeLayout.setOnClickListener(this);
        mQRCodeImg.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.receipt_qrcode_layout:
            case R.id.receipt_qrcode_img:

                Intent intent = new Intent(getActivity(), CaptureActivity.class);
                /*ZxingConfig是配置类
                 *可以设置是否显示底部布局，闪光灯，相册，
                 * 是否播放提示音  震动
                 * 设置扫描框颜色等
                 * 也可以不传这个参数
                 * */
                ZxingConfig config = new ZxingConfig();
                config.setPlayBeep(true);//是否播放扫描声音 默认为true
                config.setShake(true);//是否震动  默认为true
                config.setDecodeBarCode(false);//是否扫描条形码 默认为true
                config.setReactColor(R.color.white);//设置扫描框四个角的颜色 默认为淡蓝色
                config.setFrameLineColor(R.color.white);//设置扫描框边框颜色 默认无色
                config.setFullScreenScan(true);//是否全屏扫描  默认为true  设为false则只会在扫描框中扫描
                intent.putExtra(Constant.INTENT_ZXING_CONFIG, config);
                startActivityForResult(intent, REQUEST_CODE_SCAN);

                break;
            default:
        }
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        // 扫描二维码/条码回传
        if (requestCode == REQUEST_CODE_SCAN && resultCode == RESULT_OK) {
            if (data != null) {
                String content = data.getStringExtra(Constant.CODED_CONTENT);
//                result.setText("扫描结果为：" + content);
                Intent intent = new Intent(getActivity(), UploadReceipt.class);
                intent.putExtra("id", content);
                startActivity(intent);
            }
        }
    }
}
